package com.begye.smartloan.apptest.renzheng.dialog

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.DiaDocumentSourceTypeBinding
import com.begye.smartloan.apptest.tool.addTouch

class DocumentSourceTypeDialogFragment(val onClickAction: (Int) -> Unit) : DialogFragment() {

    private var _binding: DiaDocumentSourceTypeBinding? = null
    private val binding get() = _binding!!

    private var _type = TYPE_TAKE_PIC

    companion object {
        const val TYPE_TAKE_PIC = 0
        const val TYPE_PHOTO_ALBUM = 1
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val layout = inflater.inflate(R.layout.dia_document_source_type, container, false)
        _binding = DiaDocumentSourceTypeBinding.bind(layout)
        return layout
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Theme_Bottom_Dialog)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)

        binding.diaDocumentSourceTypeClose.setOnClickListener {
            dismiss()
        }

        binding.diaDocumentSourceTypeSubmit.setOnClickListener {
            onClickAction.invoke(_type)

            //
            addTouch(type = "document_type_dialog_commit_click", _type.toString())

            dismiss()
        }

        binding.diaDocumentSourceTypeTakePicContent.setOnClickListener {
            _type = TYPE_TAKE_PIC

            binding.diaDocumentSourceTypeTakePicContent.setBackgroundResource(R.drawable.layer_c12_000000)
            binding.diaDocumentSourceTypePhotoAlbumContent.setBackgroundResource(R.drawable.layer_c12_f8f8f8)

            binding.diaDocumentSourceTypeTakePicTitle.setTextColor(Color.parseColor("#ffffff"))
            binding.diaDocumentSourceTypePhotoAlbumTitle.setTextColor(Color.parseColor("#000000"))

            binding.diaDocumentSourceTypeTakePicCheckbox.setImageResource(R.drawable.dres_75)
            binding.diaDocumentSourceTypePhotoAlbumCheckbox.setImageResource(R.drawable.dres_77)
        }

        binding.diaDocumentSourceTypePhotoAlbumContent.setOnClickListener {
            _type = TYPE_PHOTO_ALBUM

            binding.diaDocumentSourceTypeTakePicContent.setBackgroundResource(R.drawable.layer_c12_f8f8f8)
            binding.diaDocumentSourceTypePhotoAlbumContent.setBackgroundResource(R.drawable.layer_c12_000000)

            binding.diaDocumentSourceTypeTakePicTitle.setTextColor(Color.parseColor("#000000"))
            binding.diaDocumentSourceTypePhotoAlbumTitle.setTextColor(Color.parseColor("#ffffff"))

            binding.diaDocumentSourceTypeTakePicCheckbox.setImageResource(R.drawable.dres_77)
            binding.diaDocumentSourceTypePhotoAlbumCheckbox.setImageResource(R.drawable.dres_75)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()
        val dialog = dialog ?: return
        val window = dialog.window ?: return

        window.attributes.gravity = Gravity.CENTER
        window.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )

        //
        addTouch(type = "document_type_dialog_show")
    }
}